package com.share.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.share.entity.Storage;
import com.share.mapper.StorageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author caifan
 * @date 2020/1/1
 */
@Service
public class StorageService {
    @Resource
    private StorageMapper storageMapper;

    @Transactional(rollbackFor = Exception.class)
    public void deduct(String commodityCode, Integer count) {
        if (commodityCode.equals("product-2")) {
            throw new RuntimeException("异常:模拟业务异常:Storage branch exception");
        }
        Storage storage = storageMapper.selectOne(new QueryWrapper<Storage>().lambda().eq(Storage::getCommodityCode, commodityCode));
        storage.setCount(storage.getCount() - count);
        storageMapper.updateById(storage);
    }
}
